var appid = 'kou72um0ioak1kubrz0klwx8a7s3l261mgt54vbyotnblnby';
var appkey = 'o56bexx1vr522x7ygfkvi7efvbhhmo6x2oaf3txb75wi2509';
AV.initialize(appid, appkey);
var Version = AV.Object.extend('Version');
var version_query = new AV.Query(Version);
version_query.descending('index');
version_query.addDescending('createdAt');
version_query.equalTo('publish', true);

//plus.nativeUI.showWaiting("检测更新...");
version_query.first({
  success: function(version) {
  	
  	plus.runtime.getProperty(plus.runtime.appid,function(inf){
  		//plus.nativeUI.closeWaiting();
        
        wgtVer=inf.version;
        console.log("当前应用版本："+wgtVer);
        
        if (version && (wgtVer < version.get("code")) && (version.get("type") == "apk")) {
        		console.log("最新应用版本："+version.get("code"));
        		plus.nativeUI.confirm( version.get("message"), function(event){
        			if ( 0==event.index ) {
					if (mui.os.ios) {
        					plus.runtime.openURL( "https://itunes.apple.com/cn/app/id927289999" );
        				} else {
        					console.log("下载升级包：" + version.get("url"));
        					downApk(version.get("url"))
        				}
				}
			}, "发现新版本", ["立即更新","取消"] );
        		return true;
        };
        if (version && (wgtVer < version.get("code")) && (version.get("type") == "wgt")) {
        		console.log("最新应用版本："+version.get("code"));
        		console.log("下载升级包：" + version.get("url"));
        		downWgt(version.get("url"))
        		return true;
        };
        console.log("无可用升级包");
    });
  },
  error: function(error) {
    console.log('Error: ' + error.code + ' ' + error.message);
  }
});

// 下载apk文件
function downApk(apkUrl) {
	plus.nativeUI.showWaiting("下载升级文件...");
    plus.downloader.createDownload( apkUrl, {filename:"_doc/update/"}, function(d,status){
        if ( status == 200 ) { 
            console.log("下载升级包成功："+d.filename);
            plus.runtime.install(d.filename); // 安装wgt包
        } else {
            console.log("下载升级包失败！");
            plus.nativeUI.alert("下载wgt失败！");
        }
        plus.nativeUI.closeWaiting();
    }).start();
}

// 下载wgt文件
function downWgt(wgtUrl){
    //plus.nativeUI.showWaiting("下载升级文件...");
    plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
        if ( status == 200 ) { 
            console.log("下载升级包成功："+d.filename);
            installWgt(d.filename); // 安装wgt包
        } else {
            console.log("下载升级包失败！");
            //plus.nativeUI.alert("下载wgt失败！");
        }
        //plus.nativeUI.closeWaiting();
    }).start();
}

// 更新应用资源
function installWgt(path){
    //plus.nativeUI.showWaiting("安装升级文件...");
    plus.runtime.install(path,{},function(){
        //plus.nativeUI.closeWaiting();
        console.log("安装升级文件成功！");
        var storage = window.localStorage;
        delete storage["clear_cache_at"];
        if (mui.os.android) {
        		plus.nativeUI.alert("喵~饭盒已经更新了呢！",function(){
        			plus.runtime.restart();
        		});
        }
    },function(e){
        //plus.nativeUI.closeWaiting();
        console.log("安装升级文件失败["+e.code+"]："+e.message);
        //plus.nativeUI.alert("安装升级文件失败["+e.code+"]："+e.message);
    });
}